package com.ycy.ss;

import java.io.BufferedReader;
import java.io.IOException;
import java.text.SimpleDateFormat;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;


import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.urlshortener.Urlshortener;
import com.google.api.services.urlshortener.model.Url;
import com.ycy.util.httpUtils.HttpRequest;
import com.ycy.util.httpUtils.HttpResponse;
import com.ycy.util.httpUtils.HttpUtil;

public class SSUtil{
	private static Logger logger = LogManager.getLogger(SSUtil.class);

	public static boolean JOB_LOCK = false;

	private static HttpTransport _transport = new NetHttpTransport();

	private static JacksonFactory _jsonFactory = new JacksonFactory();

	private static String _cutStringWith = "..";

	public static String shortLinkbyUrl(String longUrl){
		if (longUrl == null || longUrl.startsWith("http://is.gd/") || longUrl.startsWith("http://is.gd/")) {
			return longUrl;
		}

		String shortUrl = null;
		BufferedReader br = null;
		try {
			HttpUtil httpUtil = new HttpUtil().setPost(false);
			HttpRequest req = new HttpRequest();
			req.setUrl("http://is.gd/create.php?format=simple&url=" + longUrl).setNeedCookie(false).setNeedHeaderMap(false);
			HttpResponse resp = httpUtil.request(req);
//			shortUrl = JSONObject.fromObject(resp.getBody()).getString("shortUrl");
			shortUrl = resp.getBody();
		} catch (Exception e) {
			logger.warn("", e);
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					logger.warn("", e);
				}
			}
		}

		return shortUrl;
	}

	private static Urlshortener newUrlshortener(Credential credential){
		return new Urlshortener.Builder(_transport, _jsonFactory, credential).build();
	}

	public static String shortGoogleUrl(Credential credential, String longUrl){
		if (credential == null || longUrl == null || longUrl.startsWith("http://goo.gl/")) {
			return longUrl;
		}

		Urlshortener urlShortener = newUrlshortener(credential);
		Url url = new Url();
		url.setLongUrl(longUrl);
		String shortUrl = null;
		try {
			shortUrl = urlShortener.url().insert(url).execute().getId();
		} catch (IOException e) {
			logger.warn("", e);
		}
		return shortUrl;
	}

	public static String longGoogleUrl(Credential credential, String shortUrl){
		if (credential == null || shortUrl == null || !shortUrl.startsWith("http://goo.gl/")) {
			return shortUrl;
		}
		Urlshortener urlShortener = newUrlshortener(credential);
		String longUrl = null;
		try {
			longUrl = urlShortener.url().get(shortUrl).execute().getLongUrl();
		} catch (IOException e) {
			logger.warn("", e);
		}
		return longGoogleUrl(credential, longUrl);
	}

	public static String cutString(String str, int length){
		if (str == null) {
			return "";
		} else if (str.length() <= length) {
			return str;
		} else {
			return str.substring(0, Math.max(0, length - _cutStringWith.length())) + _cutStringWith;
		}
	}

	public static String getCurrentGmtTime(int gmt, String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		sdf.setTimeZone(java.util.TimeZone.getTimeZone("GMT+" + gmt));
		return sdf.format(new java.util.Date());
	}

//	public static void main(String[] args){
//		System.out.println(shortLinkbyUrl("http://www.google.com/"));
//	}

}
